﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Abp.Extension.CodeGenerater.Winform.Model;
using Abp.Extension.CodeGenerater.Winform.Service;
using Abp.Extension.CodeGenerater.Winform.Service.assembly;

namespace Abp.Extension.CodeGenerater.Winform
{
    public partial class mainForm : Form
    {

        private IList<string> funcs = new List<string>();
        private AssemblyService _assemblyService;
        public mainForm()
        {
            InitializeComponent();
            Init();
        }

        #region 初始化

        private void Init()
        {
            openFileDialog1.CheckPathExists = true;
            
            #region 数据类型
            IList<CodeType> codeTypes = new List<CodeType>()
            {
                new CodeType(){Id = 1,Name = "服务"},
                new CodeType(){Id = 2,Name = "实体"}
            };
            comboBox1.DataSource = codeTypes;
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "Name";
            #endregion
        }
        #endregion

        #region 工具栏
        /// <summary>
        /// 选择生成代码类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CodeType codeType = (CodeType)comboBox1.SelectedItem;
            if ((int)CodeTypes.服务 == codeType.Id)
            {
                groupBox_func.Visible = true;
                groupBox_func.Refresh();
            }
            else
            {
                groupBox_func.Visible = false;
                groupBox_func.Refresh();
            }
        }
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_create_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(tb_fileName.Text))
            {
                MessageBox.Show("请输入数据类型");
                return;
            }

            CodeType codeType = (CodeType)comboBox1.SelectedItem;
            if ((int)CodeTypes.服务 == codeType.Id)
            {
                //服务
                {

                    IList<string> codes = new AppServiceCreator(tb_fileName.Text)
                    .Init(funcs.ToArray())
                    .Create();
                    tabControl_codeContent.TabPages[0].Text = tb_fileName.Text;
                    textBox_code.Text = codes[0];
                    TabPage tp = new TabPage();
                    tp.Text = "I" + tb_fileName.Text;
                    TextBox tb = new TextBox();
                    tb.Dock = DockStyle.Fill;
                    tb.Multiline = true;
                    tb.Text = codes[1];
                    tp.Controls.Add(tb);
                    tabControl_codeContent.TabPages.Add(tp);
                }

            }
        }
        /// <summary>
        /// 选择或取消生成方法时（生成服务代码时）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_func_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (cb != null && cb.Checked)
            {
                funcs.Add(cb.Name.Split('_')[2]);
            }
            else
            {
                funcs.RemoveAt(funcs.IndexOf(cb.Name.Split('_')[2]));
            }
        }
        /// <summary>
        /// 点击选择DLL文件
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tb_DllPath.Text = openFileDialog1.FileName;
                _assemblyService = new AssemblyService(openFileDialog1.FileName).Decompiler();
            }
        }
        /// <summary>
        /// 数据类型输入框失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_fileName_Leave(object sender, EventArgs e)
        {
            string text = tb_fileName.Text;
            if (string.IsNullOrEmpty(text)) return;
            
        }

        #endregion


    }
}
